package com.guoxin.thread.lock;

/**
 * 写入线程
 *
 * @author guoxin
 * @date 2019/6/28
 */
@SuppressWarnings("all")
public class InputThread extends Thread {
    private final Resource resource;

    public InputThread(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        int count = 0;
        while (true) {
            try {
                resource.lock.lock();
                if (resource.flag) {
                    resource.condition.await();
                }
                if (count % 2 == 0) {
                    resource.name = "张三";
                    resource.six = "男";
                } else {
                    resource.name = "李四";
                    resource.six = "女";
                }
                count++;
                resource.flag = true;
                resource.condition.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                resource.lock.unlock();
            }
        }
    }
}
